package com.ttsx.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ttsx.mapper.ProductMapper;
import com.ttsx.pojo.dto.product.ProductQueryDTO;
import com.ttsx.pojo.entity.Product;
import com.ttsx.service.ProductDetailService;
import com.ttsx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @author lucky
 * @description 针对表【ttsx_product(产品表)】的数据库操作Service实现
 * @createDate 2025-03-01 17:41:06
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Autowired
    private ProductDetailService productDetailService;


    @Override
    public List<Product> getCategoryListByProductId(Long categoryId) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getCategoryId, categoryId);
        return baseMapper.selectList(lqw);
    }

    @Override
    public PageInfo<Product> getPage(Integer pIndex, Integer pSize, ProductQueryDTO productQueryDTO) {
        PageHelper.startPage(pIndex, pSize);

        Long categoryId = productQueryDTO.getCategoryId();
        String name = productQueryDTO.getName();
        Double priceStart = productQueryDTO.getPriceStart();
        Double priceEnd = productQueryDTO.getPriceEnd();

        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(categoryId), Product::getCategoryId, categoryId);
        lqw.like(StrUtil.isNotBlank(name), Product::getName, name);
        lqw.ge(ObjectUtil.isNotNull(priceStart), Product::getPrice, priceStart);
        lqw.le(ObjectUtil.isNotNull(priceEnd), Product::getPrice, priceEnd);

        String orderBy = productQueryDTO.getOrderBy();
        if (StrUtil.isNotBlank(orderBy)) {
            if ("price".equals(orderBy)) {
                lqw.orderByDesc(Product::getPrice);
            } else if ("popularity".equals(orderBy)) {
                lqw.orderByDesc(Product::getPopularity);
            }
        } else {
            lqw.orderByDesc(Product::getCreateTime);

        }

        List<Product> products = baseMapper.selectList(lqw);

        return new PageInfo<>(products, 5);
    }


    public Product getById(Serializable id) {
        Product product = super.getById(id);

        if(product != null){
            product.setDetails(productDetailService.getByProductId(product.getId()));
        }

        return product;
    }


    @CacheEvict(value = "category", allEntries = true)  // 清空缓存
    @Override
    public boolean save(Product entity) {
        return super.save(entity);
    }

    @CacheEvict(value = "category", allEntries = true)  // 清空缓存
    @Override
    public boolean updateById(Product entity) {
        return super.updateById(entity);
    }

    @CacheEvict(value = "category", allEntries = true)  // 清空缓存
    @Override
    public boolean removeById(Product entity) {
        return super.removeById(entity);
    }

    @CacheEvict(value = "category", allEntries = true)  // 清空缓存
    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return super.removeBatchByIds(list);
    }
}




